var sec_to_time = require('timeConvert')
var userSkills = require('userSkills')
var UserData = require('UserData')
var COMMON_VALUE = require('Common')
cc.Class({
    extends: cc.Component,

    properties: {
        RestaurantListNode: {
            default: null,
            type: cc.Node,
        },
        earthShakeAudioClip: {
            default: null,
            type: cc.AudioClip
        },
        Camera: {
            default: null,
            type: cc.Node
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        this.coolDownLabel = this.node.getChildByName('Label')
        this.coolDownTotalTime = COMMON_VALUE.COOLDOWN_EARTHQUAKE
        this.skillDuration = COMMON_VALUE.DURATION_EARTHQUAKE

        this.coolDownTimeLeft = UserData.runtimeState.userSkillsCoolDownList.earthquake
        if (this.coolDownTimeLeft == 0) {
            this.earthShakeEnable = true
        } else {
            this.earthShakeEnable = false
        }
    },
    shareDecorator() {
        if (!this.earthShakeEnable) {
            try {
                let setting = {
                    title: '你禁得住诱惑吗？',
                    imageUrl: 'http://img.wenzhangba.com/allimg/180806/1401-1PP60524130.png',
                    query: 'whoami=bbbbt'
                }

                if (UserData.runtimeState && UserData.runtimeState.shareSettings) {
                    let randomIndex = Math.floor(Math.random() * UserData.runtimeState.shareSettings.length)
                    setting = UserData.runtimeState.shareSettings[randomIndex]
                }
                wx.shareAppMessage(setting)
            } catch (err) {
                console.log(err)
            }

            if (Math.random() < 0.8) {
                setTimeout(() => {
                    this.earthShakeEnable = true
                    this.shake()
                }, 2000)
            }
        } else {
            this.shake()
        }
    },
    shake() {
        if (this.earthShakeEnable) {
            cc.audioEngine.play(this.earthShakeAudioClip)
            userSkills.updateUserSkill("earthshake")
            this.coolDownTimeLeft = this.coolDownTotalTime
            this.earthShakeEnable = false
            this.time = 0
            this.earthShaking = true
            this.amplitude = 50
            this.duration = 4
            for (let i = 0; i < this.RestaurantListNode.children.length; i++) {
                this.RestaurantListNode.children[i].getComponent('ResCoin').emitCoin()
                this.RestaurantListNode.children[i].getComponent('ResCoin').emitCoin()
            }
        }
    },
    easeExpOut(t, b, c, d) {
        return (t == d) ? b + c : c * (-Math.pow(2, -10 * (d - t) / d) + 1) + b;
    },
    update(dt) {

        if (this.earthShaking === true) {
            this.time += dt
            this.Camera.x = Math.random() * this.amplitude * (this.duration - this.time) / this.duration
            this.Camera.y = Math.random() * this.amplitude * (this.duration - this.time) / this.duration //* this.easeExpOut(this.time,1,this.amplitude,this.duration)

            if (this.time > this.duration) {
                this.earthShaking = false
                this.Camera.x = 0
                this.Camera.y = 0
            }
        }
        if (!this.earthShakeEnable) {
            if (this.coolDownTimeLeft > 0) {
                this.coolDownTimeLeft -= dt
                this.coolDownLabel.active = true
                this.coolDownLabel.getComponent(cc.Label).string = sec_to_time(this.coolDownTimeLeft, 'shrink')
            } else {
                this.coolDownTimeLeft = 0
                this.coolDownLabel.getComponent(cc.Label).string = ""
                //this.coolDownLabel.active =false
                this.earthShakeEnable = true
            }
        }
        let fillRange = (this.skillDuration - (this.coolDownTotalTime - this.coolDownTimeLeft)) / this.skillDuration
        if (this.coolDownTotalTime < this.coolDownTimeLeft || this.earthShakeEnable) {
            fillRange = 1
        } else if (fillRange < 0) {
            fillRange = 0
        }
        if (fillRange >= 0 && fillRange <= 1) {
            this.node.getChildByName('mask').getComponent(cc.Sprite).fillRange = fillRange
        } else {
            this.node.getChildByName('mask').getComponent(cc.Sprite).fillRange = 1
        }

    },
});
